package com.example.testrecorder.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.testrecorder.R;
import com.example.testrecorder.domain.SelectItem;

import java.util.List;

/**
 * @author Yingyong Lao
 * 创建时间 2021/6/8 21:28
 * @version 1.0
 */
public class SelectDialog extends Dialog {
    private TextView titleTv;
    private ImageButton closeBtn;
    private ListView lv;
    private List<SelectItem> list;
    private Context context;
    public SelectDialog(@NonNull Context context, List<SelectItem> list, AdapterView.OnItemClickListener listener) {
        super(context, R.style.custom_dialog);
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.select_dialog, null);
        this.context=context;
        titleTv=v.findViewById(R.id.titleTv);
        closeBtn=v.findViewById(R.id.closeBtn);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectDialog.this.dismiss();
            }
        });
        this.list=list;
        lv=v.findViewById(R.id.lv);
        SelectItemAdapter selectItemAdapter = new SelectItemAdapter();
        lv.setAdapter(selectItemAdapter);
        lv.setOnItemClickListener(listener);
        setCancelable(false);
        setContentView(v);
        show();
    }





    private class SelectItemAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            SelectItem selectItem = list.get(position);
            ViewHolder viewHolder=null;
            if(convertView==null){
                convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.select_item, parent, false);
                viewHolder=new ViewHolder();
                viewHolder.imageView=convertView.findViewById(R.id.iv);
                viewHolder.textView=convertView.findViewById(R.id.tv);
                convertView.setTag(viewHolder);
            }else {
                viewHolder= (ViewHolder) convertView.getTag();
            }
            viewHolder.imageView.setImageResource(selectItem.getImgId());
            viewHolder.textView.setText(selectItem.getItemTxt());
            return convertView;
        }

        class ViewHolder{
            ImageView imageView;
            TextView textView;
        }
    }


}
